{include file="../app/common/admin/lib/header.html" title="自定义菜单"}
  <div class="menubox" style="width:auto;">
    <div class="menu-top clearfix">
      <div class="btn-area right">
        {if $store_id}
        <a href="{:url('/appmenu/bind_menu/index')}" title="成功授权了菜单接口，才能同步信息" class="btn btn-primary btn-sm js_bindMenu">重新授权</a>
        <a href="javascript:void(0);" title="同步到微信公众号" class="btn btn-success btn-sm js_syncMenu">同步微信菜单</a>
        <a href="javascript:void(0);" title="取消微信公众号的菜单" class="btn btn-danger btn-sm js_clearMenu">取消微信菜单</a>
        {else}
        <!--<a href="javascript:void(0);" title="同步到微信公众号" class="btn btn-success btn-sm js_syncMenu">同步店铺微信菜单</a>-->
        <!--<a href="javascript:void(0);" title="取消微信公众号的菜单" class="btn btn-danger btn-sm js_clearSpecialMenu">取消店铺微信菜单</a>-->
        {/if}
        <a href="{:url('/appmenu/admin/sort_menu')}" title="重新为菜单排序" class="btn btn-default btn-sm">拖动排序</a>
        <a href="javascript:void(0);" title="预览菜单的真实效果" class="btn btn-info btn-sm js_showPreview">菜单预览</a>
      </div>
      <span class="tit5">自定义菜单设置管理</span>
    </div>
    <dl class="alert" style="margin-top:20px;">
      <dt>特别提示：</dt>
      <dd>
        <p>你可以创建最多<span class="color"> 3 个一级菜单</span>，每个一级菜单下可以创建最多<span class="color"> 5 个二级菜单</span></p>
        <p>编辑中的菜单不会马上被用户看到，可以放心调试。点击右上角的“同步微信菜单”才会使更改生效。</p>
        <p>点击“同步微信菜单”同步成功后，一般不会立即生效，微信官方说法是24小时以内生效。可试着用取消关注重新关注的方式来查看效果。</p>
      </dd>
    </dl>
    <div class="menu-main clearfix">
      {foreach $menu_lenth_arr as $k=>$v}
      <div class="level {if $k == 2}nomr{/if}">
        {/*真实的菜单体*/}
        <div class="showmenu menuOuterBox">
          <dl class="menu-list">
            {/*父级*/}
            <dt class="clearfix js_parentMenu js_menuDD" data-id="{$menu_list[$k]['id']}" data-content-type="{$menu_list[$k]['type']}">
              {/*父级没有时的占位*/}
              <div class="bar clearfix js_parentPlaceBox" {if isset($menu_list[$k])} style="display:none;" {/if}>
                <span class="menutit left">添加一级菜单</span>
                <a href="javascript:void(0);" class="btn btn-success btn-xs right js_addParentMenuBtn" style="display:none;">添加</a>
              </div>
              {/*父级item*/}
              <div class="barfill clearfix js_menuItem" {if !isset($menu_list[$k])}style="display:none;"{/if}>
                <span class="menutit fs18 left">{$menu_list[$k]['title']}</span>
                <div class="icon-shop right">
                  <a href="javascript:void(0);" title="添加子菜单" class="icons icon-add2 {if $menu_list[$k]['son_list'] && count($menu_list[$k]['son_list']) >= 5}icon-add2-disabled{/if} js_addSonBtn"></a>
                  <a href="javascript:void(0);" title="设置菜单回复" class="icons icon-link {if $menu_list[$k]['son_list']}icon-link-disabled{/if} js_setBtn"></a>
                  <a href="javascript:void(0);" title="编辑菜单标题" class="icons icon-edit js_editBtn"></a>
                  <a href="javascript:void(0);" title="删除菜单" class="icons icon-del  js_delBtn"></a>
                </div>
              </div>
              {/*父级inputBox*/}
              <div class="barfill clearfix js_inputBox" style="display:none;">
                <input type="text" class="titfill left js_input" value="{$menu_list[$k]['title']}">
                <a href="javascript:void(0);" class="btn btn-success btn-xs right js_okBtn">确定</a>
              </div>
            </dt>
            {/*子级*/}
            {foreach $menu_list[$k]['son_list'] as $k1=>$v1}
              <dd class="js_sonMenu js_menuDD" data-id="{$v1['id']}" data-parent-id="{$v1['parent_id']}" data-content-type="{$v1['type']}">
                {/*子级item*/}
                <div class="submenu clearfix js_menuItem">
                  <span class="menutit left">{$v1.title}</span>
                  <div class="icon-shop right">
                    <a href="javascript:void(0);" title="设置菜单回复" class="icons icon-link js_setBtn"></a>
                    <a href="javascript:void(0);" title="编辑菜单标题" class="icons icon-edit js_editBtn"></a>
                    <a href="javascript:void(0);" title="删除菜单" class="icons icon-del js_delBtn"></a>
                  </div>
                </div>
                {/*子级iputBox*/}
                <div class="submenu clearfix js_inputBox" style="display:none;">
                  <input type="text" class="titfill left js_input" value="{$v1.title}">
                  <a href="javascript:void(0);" class="btn btn-success btn-xs right js_okBtn">确定</a>
                </div>
              </dd>
            {/foreach}
            <dd style="display:none;" class="js_sonBak js_menuDD">
              <div class="submenu clearfix js_menuItem" style="display:none;">
                <span class="menutit left">{$v1.title}</span>
                <div class="icon-shop right">
                  <a href="javascript:void(0);" title="设置菜单回复" class="icons icon-link js_setBtn"></a>
                  <a href="javascript:void(0);" title="编辑菜单标题" class="icons icon-edit js_editBtn"></a>
                  <a href="javascript:void(0);" title="删除菜单" class="icons icon-del js_delBtn"></a>
                </div>
              </div>
              <div class="submenu clearfix js_inputBox">
                <input type="text" class="titfill left js_input" value="添加一级菜单1">
                <a href="javascript:void(0);" class="btn btn-success btn-xs right js_okBtn">确定</a>
              </div>
            </dd>
          </dl>
        </div>
      </div>
      {/foreach}
    </div><!--end .menu-main-->
  </div>

{include file="../app/common/admin/lib/app_custom_select.html"}
{js href="./static/js/appCustomSelect.js"}
<script>
var appmenuResname = "{$res_name}";
var storeId        = '{$store_id}';

var miniprogramDevide = "{appmenu_config::$miniprogram_devide}";

var fillParent = '添加一级菜单';
var fillSon    = '添加子菜单';
function isSonBoxShow(){
  $('.js_parentBox').each(function(){
    if ($(this).find('.js_sonBox:visible').length) {
      $(this).find('.js_parentTitle').addClass('js_open').removeClass('js_close');

    } else {
      $(this).find('.js_parentTitle').removeClass('js_open').addClass('js_close');
    }
  });
}
function setMenuLiWidth(){
  //var totalWidth = $('.js_menuOutBox').width();
  var liNum      = $('.js_parentBox').length;
  var widthCss = '33%';
  if (liNum == 1) {
    widthCss = '100%';
    $('.js_parentBox').css({ 'width':widthCss });
    var parentWidth = $('.js_parentBox').width();
    var sonWidth1   = $('.js_sonBox').eq(0).width() + 28;
    var left1       = (parentWidth - sonWidth1) / 2;
    $('.js_sonBox').eq(0).css({ 'left':left1+'px' });
  } else if (liNum == 2) {
    widthCss = '50%';
    $('.js_parentBox').css({ 'width':widthCss });
    var parentWidth = $('.js_parentBox').width();
    var sonWidth1   =  $('.js_sonBox').eq(0).width() + 28;
    var sonWidth2   =  $('.js_sonBox').eq(1).width() + 28;
    var left1       = (parentWidth - sonWidth1) / 2;
    $('.js_sonBox').eq(0).css({ 'left':left1+'px' });
    if (sonWidth2 <= parentWidth) {
      var left2     = (parentWidth - sonWidth2) / 2;
      $('.js_sonBox').eq(1).css({ 'left':left2+'px' });
    } else {
      $('.js_sonBox').eq(1).css({ 'right':'5px' });
      $('.js_sonBox em').eq(1).css({ 'margin-left':'auto', 'left':'auto', 'right':'45px' });
    }
  } else {
    $('.js_parentBox').css({ 'width':widthCss });
    var parentWidth = $('.js_parentBox').width();
    var sonWidth1   =  $('.js_sonBox').eq(0).width() + 28;
    var sonWidth2   =  $('.js_sonBox').eq(1).width() + 28;
    var sonWidth3   =  $('.js_sonBox').eq(2).width() + 28;
    var left1       = (parentWidth - sonWidth1) / 2;
    var left2       = (parentWidth - sonWidth2) / 2;
    $('.js_sonBox').eq(0).css({ 'left':left1+'px' });
    $('.js_sonBox').eq(1).css({ 'left':left2+'px' });
    if (sonWidth3 <= parentWidth) {
      var left3     = (parentWidth - sonWidth3) / 2;
      $('.js_sonBox').eq(2).css({ 'left':left3+'px' });
    } else {
      $('.js_sonBox').eq(2).css({ 'right':'5px' });
      $('.js_sonBox em').eq(2).css({ 'margin-left':'auto', 'left':'auto', 'right':'30px' });
    }
  }
}
$(function(){
  // 把预览自定义菜单
  $('#wrap').append('<div class="float-bg hidden js_priviewBg"></div><div class="float-menu js_previewMenuBox hidden"></div>');
  $('.js_showPreview').on('click', function(){
    $('.js_priviewBg').show();
    $('.js_priviewBg').removeClass('hidden');
    $('.js_previewMenuBox').show();
    $('.js_previewMenuBox').removeClass('hidden');
    $('.js_previewMenuBox').load(siteUrl+'/appmenu/admin/load_menu_preview?store_id='+storeId, function(){
      setMenuLiWidth();
    });
  });

  $('.js_parentTitle').live('click', function(){
    $('.js_sonBox').hide();
    var thisObj = $(this);
    if (thisObj.closest('.js_parentBox').find('ul').hasClass('js_noSon')) {
      isSonBoxShow();
      return;
    }
    if (thisObj.hasClass('js_close')) {
      thisObj.removeClass('js_close').addClass('js_open');
      thisObj.closest('.js_parentBox').find('.js_sonBox').show();
    } else {
      thisObj.removeClass('js_open').addClass('js_close');
      thisObj.closest('.js_parentBox').find('.js_sonBox').hide();
    }
    isSonBoxShow();
  });
  $('.js_closePreview').live('click', function(){
    $('.js_previewMenuBox').hide();
    $('.js_priviewBg').hide();
  });
  
  // 初始菜单hover出添加按扭
  $(".js_parentPlaceBox").hover(
    function(){
      $(this).find(".js_addParentMenuBtn").show();
    },
    function(){
      $(this).find(".js_addParentMenuBtn").hide();
    }
  );
  
  // 初始时一级菜单点击增加变输入框
  $(".js_addParentMenuBtn").live('click', function(){
    $(this).closest('dt').find('.js_parentPlaceBox').hide();
    $(this).closest('dt').find('.js_inputBox').show();
    
    inputPlaceHold($(this).closest('dt').find(".js_input"), fillParent);
  });
  
  // 添加子菜单
  $(".js_addSonBtn").live('click', function(e){
    var thisClosest = $(this).closest('dl');

    if (getSonNum(thisClosest) >= 5) {
      showMsg('最多只能有5个子菜单');
      return false;
    }
    
    thisClosest.find(".js_sonBak").before(thisClosest.find(".js_sonBak").clone());
    var currObj = thisClosest.find(".js_sonBak").eq(0);
    currObj.attr({ 'data-parent-id':thisClosest.find(".js_parentMenu").attr('data-id') }).removeClass("js_sonBak").addClass("js_sonMenu").show();
    
    inputPlaceHold(currObj.find(".js_input"), fillSon);
    
    // 控制小图标样式
    if (thisClosest.find(".js_sonMenu").length > 0) {
      var parentObj = thisClosest.find(".js_parentMenu");
      parentObj.find(".js_setBtn").addClass("icon-link-disabled");
      
      if (thisClosest.find(".js_sonMenu").length >= 5) {
        parentObj.find(".js_addSonBtn").addClass("icon-add2-disabled");
      }
    }
  });
  
  // 编辑菜单
  $(".js_editBtn").live('click', function(e){
      var thisClosest = $(this).closest(".js_menuItem");
      var title = thisClosest.find('menutit').text();
      
      thisClosest.hide();
      thisClosest.next().show();
      thisClosest.next().find(".js_input").text(title);
      var fill = fillParent;
      
      // 子级
      if (thisClosest.closest(".js_parentMenu").length < 0) {
        fill = fillSon;
      }
      inputPlaceHold(thisClosest.next().find(".js_input"), fill, 1);
  });
  
  // 设置菜单
  $(".js_setBtn").live('click', function(){
    var thisObj     = $(this);
    var menuDD      = $(this).closest(".js_menuDD");
    var id          = menuDD.attr('data-id');
    var parentId    = menuDD.attr('data-parent-id');
    var contentType = menuDD.attr('data-content-type');

    $(".currEditMenu").removeClass('currEditMenu');
    menuDD.addClass('currEditMenu');
    //showMsg(contentType);
    
    if ((typeof parentId == "undefined" || parentId < 1) && menuDD.closest('dl').find(".js_sonMenu").length > 0) {
      showMsg('不能设置含有子菜单的父级菜单');
      return false;
    }
    
    // 如果已有了内容
    if (contentType > 0) {
      var msgType = contentType;
      var currId   = $(".currEditMenu").attr('data-id');
      
      var html  = '<div class="float-prompt-con">\
                   <div class="float-pic-info" style="position:relative;"">您设置菜单的回复内容如下，&nbsp;\
                    <a href="javascript:void(0);" class="js_setBtn2">点击去更改</a>\
                    <div class="linkpop js_menuSetBox2" style="display:none; left:40px; top:26px; z-index:555;">\
                      <div class="inner-linkpop">\
                        <span>\
                          <i class="arrowpop"></i>\
                          <b class="arrowpop2"></b>\
                        </span>\
                        <div class="options-list">\
                          <!-- <a class="js_menuIndexBtn" href="javascript:void(0);">关联到微官网</a>-->\
                          <a class="js_menuLinkBtn" href="javascript:void(0);">跳转到网页</a>\
                          <a class="js_menuTextBtn" href="javascript:void(0);">纯文字回复</a>\
                          <a class="js_menuSingleBtn" href="javascript:void(0);">单条图文信息</a>\
                          <a class="js_menuMultiBtn" href="javascript:void(0);">多条图文信息</a>\
                          <!--<a class="js_menuMinipBtn" href="javascript:void(0);">关联小程序</a>-->\
                          <!--<a class="js_menuCustomBtn" href="javascript:void(0);">智能回复</a>-->\
                        </div>\
                      </div>\
                    </div>\
                  </div>';
      html += '<div class="js_msgList"><img src="'+siteUrl+'/static/images/loading2.gif"></div></div>';
      
      ace.dialog({
        'title':'已设置的菜单项',
        'content':html,
        'init':function(){
          $(".aceDialog").width(650);
          if (msgType == 30 || msgType == 40 || msgType == 50) {
            $(".aceContent").height(400);
          } else {
            //$(".aceContent").css({ 'min-height':'auto' });
          }
          
          setTimeout(function(){
            resetDialogPosition($(".aceDialog"));
          }, 10);
          
          $(".js_setBtn2").live('click', function(){
            $(".js_menuSetBox2").show();
            //qDialog.close();
            //menuDD.find('.js_setBtn').click();
          });
          
          $(".js_msgList").load(siteUrl+'/appmenu/admin/show_content', { 'id':currId }, function(){
            setTimeout(function(){
              
            }, 10);
          });
        },
        'okValue':''
      });
      
      return false;
    }
    
    var _offset = thisObj.offset();
    var _top = _offset.top+27;
    var _left = _offset.left-183;
    
    $(".js_menuSetBox").css({ 'top':_top, 'left':_left }).show();
  });
  
  //  关联到首页
  $(".js_menuIndexBtn").live('click', function(){
    showConfirm('您确定要关联此菜单到微官网首页吗?', 1, function(){
      if (typeof qDialog != 'undefined') {
        qDialog.close();
      }
      
      var currId   = $(".currEditMenu").attr('data-id');
      var postData = { 'id':currId, 'content':-1, 'type':1 };
      saveMenuContent(postData);
    });
  });
  
  // 设置纯文本回复
  $(".js_menuTextBtn1").live('click', function(){
    var tmpContent = "";
    if (typeof qDialog != 'undefined') {
      var msgDiv = $(".aceDialog .js_msgList>div");
      if (msgDiv.length > 0 && msgDiv.attr('data-content-type') == 20) {
        tmpContent = msgDiv.html();
      }
      qDialog.close();
    }
    var html = '<div class="float-prompt-con"><p>在这里填写，点击菜单时返回的纯文本回复</p>\
                <p><textarea class="float-prompt-area js_content">'+tmpContent+'</textarea></p></div>';
    
    ace.dialog({
      'title':'添加文字回复',
      'content':html,
      'init':function(){
        $(".aceFooter .okBtn").css({ 'margin-left':'23px' });
      },
      'ok':function(){
        var content = $(".js_content").val();
        if (!content || !$.trim(content)) {
          showMsg('回复内容不能为空');
          return;
        }
        
        var currId   = $(".currEditMenu").attr('data-id');
        var postData = { 'id':currId, 'content':content, 'type':20 };
        saveMenuContent(postData);
      }
    });
  });
  
  //设置纯文本回复
  $(".js_menuTextBtn").live('click', function(){
    var tmpContent = "";
    if (typeof qDialog != 'undefined') {
      var msgDiv = $(".aceDialog .js_msgList>div");
      if (msgDiv.length > 0 && msgDiv.attr('data-content-type') == 20) {
        tmpContent = msgDiv.html();
      }
      qDialog.close();
    }
    var html = '<div class="float-prompt-con"><p>在这里填写，点击菜单时返回的纯文本回复</p>\
                  <div class="txtwrap">\
                    <div style="min-height:156px; height:auto;" contentEditable="true" class="txtfill js_content">'+tmpContent+'</div>\
                    <div class="txt-options clearfix">\
                      <a href="javascript:void(0);" class="txt-face-btn left js_faceBtn">表情</a>\
                      <p style="display:none;" class="txt-count right js_fontNumBox">还可输入<span>600</span>字</p>\
                    </div>\
                  </div>\
                </div>';
    
    ace.dialog({
      'title':'添加文字回复',
      'content':html,
      'init':function(){
        $(".aceFooter .okBtn").css({ 'margin-left':'23px' });
        
        weiboFace($(".js_faceBtn"), $('.txtwrap .js_content'));
      },
      'ok':function(){
        var content = $(".js_content").html();
        if (!content || !$.trim(content)) {
          showMsg('回复内容不能为空');
          return;
        }
        
        var _num = getNum($(".js_content"));
        if (_num < 1) {
          showMsg('回复的内容不能为空.');
          return;
        }
        if (_num > 600) {
          showMsg('回复的内容不能不能超过600字');
          return;
        }
        
        var currId   = $(".currEditMenu").attr('data-id');
        var postData = { 'id':currId, 'content':content, 'type':20 };
        saveMenuContent(postData);
      }
    });
  });
  
  // 设置链接回复
  $(".js_menuLinkBtn").live('click', function(){
    var tmpContent = "";
    if (typeof qDialog != 'undefined') {
      var msgDiv = $(".aceDialog .js_msgList>div");
      if (msgDiv.length > 0 && msgDiv.attr('data-content-type') == 1) {
        tmpContent = msgDiv.html();
      }
      qDialog.close();
    }
    
    var html = '<div class="float-prompt-con"><p>在这里填写，点击菜单跳转的地址。请填写正确的链接地址</p>\
                <p><input type="text" class="float-prompt-ipt1 js_link" value="'+tmpContent+'"></p></div>';
    
    ace.dialog({
      'title':'点击跳转网页',
      'content':html,
      'init':function(){
        $(".aceFooter .okBtn").css({ 'margin-left':'23px' });
      },
      'ok':function(){
        // 取链接
        var content = $.trim($(".js_link").val());
        if (!content || !$.trim(content)) {
          showMsg('菜单链接不能为空');
          return;
        }
        
        if (!checkStr(content, 'link')) {
          showMsg('链接不能为空');
          return false;
        }
        
        var currId   = $(".currEditMenu").attr('data-id');
        var postData = { 'id':currId, 'content':content, 'type':10 };
        saveMenuContent(postData);
      }
    });
  });
  
  // 设置小程序关联
  $(".js_menuMinipBtn").live('click', function(){
    var tmpAppid = '';
    var tmpPagepath = '';
    var tmpLink = '';
    if (typeof qDialog != 'undefined') {
      var msgDiv = $(".aceDialog .js_msgList>div");
      if (msgDiv.length > 0 && msgDiv.attr('data-content-type') == 60) {
        tmpAppid = $('.aceDialog .js_miniappid').html();
        tmpPagepath = $('.aceDialog .js_minipagepath').html();
        tmpLink = $('.aceDialog .js_minilink').html();
        
        //alert(tmpAppid);
      }
      qDialog.close();
    }
    
    var html = '<div class="float-prompt-con"><p>小程序appid</p>\
                <p><input type="text" class="float-prompt-ipt1 js_miniappid" value="'+tmpAppid+'"></p>\
                <p>小程序路径</p>\
                <p><input type="text" class="float-prompt-ipt1 js_pagepath" value="'+tmpPagepath+'"></p>\
                <p>备用网页 <span style="color:#aaa; font-size:12px;">旧版微信不支持小程序的情况下，会打开备用地址</span></p>\
                <p><input type="text" class="float-prompt-ipt1 js_link" value="'+tmpLink+'"></p></div>';
    
    ace.dialog({
      'title':'点击打开程序',
      'content':html,
      'init':function(){
        $(".aceFooter .okBtn").css({ 'margin-left':'23px' });
      },
      'ok':function(){
        // 取链接
        var miniappid = $.trim($(".js_miniappid").val());
        var pagepath = $.trim($(".js_pagepath").val());
        var link = $.trim($(".js_link").val());
        if (!miniappid) {
          showMsg('小程序appid不能为空');
          return;
        }
        if (!pagepath) {
          showMsg('小程序路径不能为空');
          return;
        }
        if (!link) {
          showMsg('备用网页地址不能为空');
          return;
        }
        
        if (!checkStr(link, 'link')) {
          showMsg('备用链接不正确，请以http开头');
          return false;
        }
        
        var dividStr = miniprogramDevide;
        var content = miniappid+dividStr+pagepath+dividStr+link;
        
        var currId   = $(".currEditMenu").attr('data-id');
        var postData = { 'id':currId, 'content':content, 'type':60 };
        saveMenuContent(postData);
      }
    });
  });
  
  // 设置图文回复
  $(".js_menuSingleBtn, .js_menuMultiBtn").live('click', function(){
    if (typeof qDialog != 'undefined') {
      qDialog.close();
    }
    var msgType = 30;
    var appmsgType = 1;
    if ($(this).hasClass("js_menuMultiBtn")) {
      msgType = 40;
      appmsgType = 2;
    }
    var currId   = $(".currEditMenu").attr('data-id');
    
    var html  = '<div class="float-prompt-con"><p class="float-pic-info">请选择关链到已有的图文信息，或者去&nbsp;<a target="_blank" href="'+siteUrl+'/appmsg/admin/add/'+appmsgType+'">添加新的图文信息</a></p>';
        html += '<div class="js_msgList"><img src="'+siteUrl+'/static/images/loading2.gif"></div></div>';
    
    ace.dialog({
      'title':'选择图文信息',
      'content':html,
      'init':function(){
        $(".aceDialog").width(960);
        $(".aceContent").height(400);
        $(".aceContent").css('overflow', 'auto');
        
        setTimeout(function(){
          resetDialogPosition($(".aceDialog"));
        }, 10);
        $(".js_msgList").load(siteUrl+'/appmenu/admin/get_menu_msg_list', { 'id':currId, 'type':appmsgType }, function(){
          bindMsgEvent(msgType, currId);
        });
        
        $(".js_msgList .page>a").live('click', function(e){
          e.preventDefault();
          var _href = $(this).attr('href');
          $(".js_msgList").load(_href, { 'id':currId, 'type':appmsgType }, function(){
            bindMsgEvent(msgType, currId);
          });
        });
      },
      'okValue':''
    });
  });
  
  // 添加智能回复
  $(".js_menuCustomBtn").live('click', function(){
    $(".js_menuSetBox").hide();
    
    if (typeof qDialog != 'undefined') {
      qDialog.close();
    }
    
    // msgType这里是和关键词回复不同的地方
    var msgType = 50;
    var oldHtml = $(".js_AppCustomHideBox").html();
    $(".js_AppCustomHideBox").html('');
    var tmpSelectData = [];
    qDialog = null;
    ace.dialog({
      'title':'自定义回复',
      'content':oldHtml,
      'init':function(){
        $(".aceDialog").width(680);
        //$(".aceContent").height(400);
        $(".aceContent").css('overflow', 'auto');
        
        var currType    = $(".js_typeSelect").val();
        var currSonType = $(".js_sonTypeSelect"+currType).val();
        //var currSearch  = $.trim($("#js_searchInput").val());
        var queryType   = $.trim($(".js_queryType").val());
        var queryNum   = $.trim($(".js_queryNum").val());
        $(".js_typeSelect").change(function(){
          currType = $(this).val();
          $(".js_sonTypeSelect").hide();
          $(".js_sonTypeSelect"+currType).show();
          againLoad();
          changeAddUrl();
        });
        
        $(".js_sonTypeSelect").change(function(){
          againLoad();
          changeAddUrl();
        });
        
        $(".js_queryType").change(function(){
          queryType   = $.trim($(this).val());
          if (queryType == 2) {
            $(".js_customSelectBox").show();
            $(".js_queryNum").hide();
          } else {
            $(".js_customSelectBox").hide();
            $(".js_queryNum").show();
          }
          
          setTimeout(function(){
            resetDialogPosition($(".aceDialog"));
          }, 10);
          againLoad();
        });
        
        // 拖动选择的顺序
        $(".float-choose-list").sortable({ 
          axis: "y" ,
          opacity: 0.6,
          stop:function(){
          }
        });
      },
      'ok':function(){
        if ($(".aceDialog .okBtn").attr('is_post') == 1) {
          showMsg('提交中，请稍候...');return;
        }
        var currType    = $(".js_typeSelect").val();
        var currSonType = $(".js_sonTypeSelect"+currType).val();
        var queryType   = $.trim($(".js_queryType").val());
        var queryNum    = $.trim($(".js_queryNum").val());
        //var currSearch  = $.trim($(".js_searchInput").val());
        var queryInfo = [];
        $(".js_preCustom").each(function(i){
          queryInfo[i] = $(this).attr('data-id');
        });
        var msgTitle = '文章';
        if (currType == 2) {
          msgTitle = '活动';
        }
        if (queryType == 2) {
          queryNum = queryInfo.length;
        }
        if (queryNum < 1) {
          showMsg('请至少取出一条'+msgTitle+'数据');
          return;
        }
        if (queryNum > 10) {
          showMsg(msgTitle+'数量不能多于10条');
          return;
        }
        
        var postData = { 'type':currType, 'sub_type':currSonType, 'query_type':queryType, 'query_num':queryNum, 
            'info':queryInfo, 'return_son_list':1 };
        $(".aceDialog .okBtn").text('提交中...').attr('is_post', 1);
        $.post(siteUrl+'/appmsg/admin/automsg', postData, function(json){
          $(".aceDialog .okBtn").text('确定').attr('is_post', 0);
          if (json.info != 'ok') {
            showMsg(json.info);
            return false;
          }
          
          var currId   = $(".currEditMenu").attr('data-id');
          var postData = { 'id':currId, 'content':json.content, 'type':50 };
          saveMenuContent(postData, oldHtml);
          
          //$(".js_AppCustomHideBox").html(oldHtml);
          //qDialog.close();
        }, 'json');
      },
      'close':function(){
        $(".js_AppCustomHideBox").html(oldHtml);
        //qDialog.close();
      }
    });
  });
  
  // 保存菜单
  $(".js_okBtn").live('click', function(){
    var thisClosest = $(this).closest(".js_inputBox");
    var menuDD      = $(this).closest(".js_menuDD");
    var id          = menuDD.attr('data-id');
    var parentId    = menuDD.attr('data-parent-id');
    var title       = $(this).prev(".js_input").val();
    
    if (thisClosest.attr('data-is-post') == 1) {
      showMsg('数据提交中，请稍候...')
      return false;
    }
    
    var fill = fillParent;
    // 子级
    if (thisClosest.closest(".js_parentMenu").length < 1) {
      fill = fillSon;
    }
    if (!title || !$.trim(title) || $.trim(title) == fill) {
      showMsg('菜单名称不能为空');
      return false;
    }
    
    var fontNum = getFontNum(title)
    // 父级
    if (parentId < 1 || typeof parentId == "undefined") {
      if (fontNum > 4) {
        showMsg('父级菜单不能不多于4个汉字或8个字母');
        return false;
      }
    } else {
      if (fontNum > 8) {
        showMsg('子级菜单不能不多于8个汉字或16个字母');
        return false;
      }
    }

    thisClosest.attr('data-is-post', 1);
    $.post(siteUrl+'/appmenu/admin/save', { 'title':title, 'id':id, 'parent_id':parentId, 'store_id':storeId, 'res_name':appmenuResname }, function(json){
      thisClosest.attr('data-is-post', 0);
      if (json.info != 'ok') {
        showMsg(json.info);
        return false;
      }
      
      thisClosest.hide();
      menuDD.attr({ 'data-id':json.id, 'data-parent-id':json.parent_id });
      menuDD.find(".menutit").html(json.title);
      thisClosest.prev('div').show();
    }, 'json');
  });
  
  // 删除菜单
  $(".js_delBtn").live('click', function(e){
    var menuDD   = $(this).closest(".js_menuDD");
    var id       = menuDD.attr('data-id');
    var parentId = menuDD.attr('data-parent-id');
    
    if (menuDD.attr('data-is-post') == 1) {
      showMsg('数据提交中，请稍候...')
      return false;
    }
    
    showConfirm('您确定要删除此菜单吗?', 1, function(){
      menuDD.attr('data-is-post', 1);
      $.post(siteUrl+'/appmenu/admin/delete', { 'id':id }, function(json){
        menuDD.attr('data-is-post', 0);
        if (json.info != 'ok') {
          showMsg(json.info);
          return false;
        }
        
        if (parentId > 0) {
          menuDD.remove();
        } else {
          menuDD.siblings(".js_sonMenu").remove();
          menuDD.find(".js_menuItem").hide();
          menuDD.find(".js_inputBox").hide();
          menuDD.find(".js_parentPlaceBox").show();
          menuDD.attr({ 'data-id':0, 'data-parent-id':0 });
        }
        
        // 控制小图标样式
        var thisClosest = menuDD.closest("dl");
        if (thisClosest.find(".js_sonMenu").length < 5) {
          var parentObj = thisClosest.find(".js_parentMenu");
          parentObj.find(".js_addSonBtn").removeClass("icon-add2-disabled");
          
          if (thisClosest.find(".js_sonMenu").length <= 0) {
            parentObj.find(".js_setBtn").removeClass("icon-link-disabled");
          }
        }
      }, 'json');
    });
  });
  
  // 同步菜单
  $(".js_syncMenu").click(function(e){
    e.preventDefault();
    if ($(".js_syncMenu").attr('data-is-post') == 1) {
      // showMsg('数据提交中，请稍候...')
      // return false;
    }
    
    showConfirm('您确定要同步菜单信息到微信公众号吗?', 1, function(){
      $(".js_syncMenu").attr('data-is-post', 1);
      
      $.post(siteUrl+'/appmenu/sync_menu/sync', { 'store_id':storeId, 'res_name':appmenuResname }, function(json){
        $(".js_syncMenu").attr('data-is-post', 0);
        
        if (json.info != 'ok') {
          showMsg(json.info);
          return false;
        }
        
        showMsg('同步成功', 1);
      }, 'json');
    });
  });
  
  // 取消菜单
  $(".js_clearMenu").click(function(e){
    e.preventDefault();
    if ($(".js_clearMenu").attr('data-is-post') == 1) {
      showMsg('数据提交中，请稍候...')
      return false;
    }
    
    showConfirm('您确定要取消微信公众号的自定义菜单吗?', 1, function(){
      $(".js_clearMenu").attr('data-is-post', 1);
      
      $.post(siteUrl+'/appmenu/sync_menu/clear', { }, function(json){
        $(".js_clearMenu").attr('data-is-post', 0);
        
        if (json.info != 'ok') {
          showMsg(json.info);
          return false;
        }
        
        showMsg('清除成功', 1);
      }, 'json');
    });
  });
  
  // 取消店铺个性菜单
  $(".js_clearSpecialMenu").click(function(e){
    e.preventDefault();
    if ($(".js_clearSpecialMenu").attr('data-is-post') == 1) {
      showMsg('数据提交中，请稍候...')
      return false;
    }
    
    showConfirm('您确定要取消微信公众号的自定义菜单吗?', 1, function(){
      $(".js_clearSpecialMenu").attr('data-is-post', 1);
      
      $.post(siteUrl+'/appmenu/admin/sync_menu/clear_specail', { 'store_id':storeId }, function(json){
        $(".js_clearSpecialMenu").attr('data-is-post', 0);
        
        if (json.info != 'ok') {
          showMsg(json.info);
          return false;
        }
        
        showMsg('清除成功', 1);
      }, 'json').error(function(){
        $(".js_clearSpecialMenu").attr('data-is-post', 0);
      });
    });
  });
});

// 取得子菜单的数量
function getSonNum(thisClosest)
{
  return thisClosest.find(".js_sonMenu").length;
}

// 保存菜单内容
function saveMenuContent(_data, oldHtml)
{
  var currEditMenu   = $(".currEditMenu");
  
  if (currEditMenu.attr('data-is-post') == 1) {
    showMsg('数据提交中，请稍候...')
    return false;
  }
  currEditMenu.attr('data-is-post', 1);
  $.post(siteUrl+'/appmenu/admin/save_content', _data, function(json){
    currEditMenu.attr('data-is-post', 0);
    if (json.info != 'ok') {
      showMsg(json.info);
      return false;
    }
    
    //@todo 成功操作后的动作
    currEditMenu.attr('data-content-type', _data.type);
    
    if (oldHtml) {
      $(".js_AppCustomHideBox").html(oldHtml);
    }
    
    showMsg('操作成功', 1);
    if (typeof qDialog != 'undefined') {
      qDialog.close();
    }
  }, 'json');
}

function bindMsgEvent(msgType, currId)
{
  $(".js_msgList>li").unbind('click');
  // 选择某一条图文做为回复
  $(".js_msgList>li").bind('click', function(e){
    e.preventDefault();
    var thisObj = $(this);
    if (!thisObj.hasClass('curr')) {
      var id = thisObj.attr('data-id');
      if (!id) {
        showMsg('请选择一条信息');
        return false;
      }
      
      showConfirm('您确定要选择该图文信息吗?', 1, function(){
        $.post(siteUrl+'/appmenu/admin/save_content', { 'id':currId, 'content':id, 'type':msgType }, function(json){
          if (json.info != 'ok') {
            showMsg(json.info);
            return false;
          }
          
          var currEditMenu   = $(".currEditMenu");
          currEditMenu.attr('data-content-type', msgType);
          
          $(".js_msgList li.curr").removeClass('curr');
          thisObj.addClass('curr');
          
          qDialog.close();
          //window.location.href = siteUrl+'/appmenu/controller/';
        }, 'json');
      });
    }
  });
}
</script>
{include file="../app/common/admin/lib/footer.html"}